﻿using System.Data;
using System.Data.SqlClient;

namespace Codeflows.Tracking.BuiltIn
{
    public static class SqlExtensions
    {
         public static SqlCommand WithQuery(this SqlCommand command, string query)
         {
             command.CommandText = query;

             return command;
         }

        public static SqlCommand WithParameter(this SqlCommand command, string key, object value)
        {
            command.Parameters.AddWithValue("@" + key, value);

            return command;
        }

        public static byte[] GetByteArray(this IDataRecord record, int index)
        {
            // ReSharper disable AssignNullToNotNullAttribute
            var length = record.GetBytes(index, 0, null, 0, 0);
            // ReSharper restore AssignNullToNotNullAttribute
            var buffer = new byte[length];
            // ReSharper disable ReturnValueOfPureMethodIsNotUsed
            record.GetBytes(index, 0L, buffer, 0, (int)length);
            // ReSharper restore ReturnValueOfPureMethodIsNotUsed
            return buffer;
        }
    }
}